using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AntEater.ComponentModel.BuildElements.Design
{
    /// <summary>
    /// Provides a control that allows a user to select a Target.
    /// </summary>
    public partial class TargetCollectionTypeEditorUI : UserControl
    {
        /// <summary>
        /// Occurs when the user has completed editing.
        /// </summary>
        public event EventHandler EditComplete;

        private TargetCollection _selectedTargets;

        /// <summary>
        /// Gets the selected targets.
        /// </summary>
        /// <value>The selected targets.</value>
        public TargetCollection SelectedTargets {
            get { return _selectedTargets; }
        }

        private BuildFileDefinition _buildFileDefinition;

        /// <summary>
        /// Gets or sets the build file definition.
        /// </summary>
        /// <value>The build file definition.</value>
        public BuildFileDefinition BuildFileDefinition {
            get { return _buildFileDefinition; }
            set { _buildFileDefinition = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:TargetCollectionTypeEditorUi"/> class.
        /// </summary>
        /// <param name="targets">The targets.</param>
        /// <param name="buildFileDefinition">The build file definition.</param>
        public TargetCollectionTypeEditorUI(TargetCollection targets, BuildFileDefinition buildFileDefinition) {
            InitializeComponent();
            _selectedTargets = targets;
            _buildFileDefinition = buildFileDefinition;

            this.Refresh();
        }

        /// <summary>
        /// Forces the control to invalidate its client area and immediately redraw itself and any child controls.
        /// </summary>
        /// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        public override void Refresh() {

            this.RefreshListBox();
            
            base.Refresh();
        }

        /// <summary>
        /// Refreshes the collection editor.
        /// </summary>
        private void RefreshListBox() {

            targetsListView.Groups.Clear();

            ListViewGroup missingTargetsGroup = new ListViewGroup("Missing Targets");
            ListViewGroup localTargetsGroup = new ListViewGroup("Local Targets");
            ListViewGroup importedTargetsGroup = new ListViewGroup("Imported Targets");

            List<string> addedTargets = new List<string>();

            targetsListView.Groups.Add(missingTargetsGroup);
            targetsListView.Groups.Add(localTargetsGroup);
            targetsListView.Groups.Add(importedTargetsGroup);

            foreach (TargetBuildElement selectableTarget in this.BuildFileDefinition.Targets) {

                ListViewItem item = new ListViewItem(selectableTarget.Name);

                item.Checked = this.SelectedTargets.Contains(selectableTarget.Name);

                if (!selectableTarget.IsImported) {
                    item.Group = localTargetsGroup;
                }
                else {
                    item.Group = importedTargetsGroup;
                }

                item.ImageKey = "Target";

                targetsListView.Items.Add(item);
                addedTargets.Add(item.Text);
            }

            //Add Targets that are missing in Build File to ListView
            foreach (string seletedTarget in this.SelectedTargets) {
                if (!addedTargets.Contains(seletedTarget)) {

                    ListViewItem item = new ListViewItem(seletedTarget);

                    item.Checked = true;

                    item.Group = missingTargetsGroup;

                    item.ImageKey = "MissingTarget";

                    targetsListView.Items.Add(item);
                }
            }

            targetsListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        /// <summary>
        /// Called when [edit completed].
        /// </summary>
        protected void OnEditComplete() {
            if (this.EditComplete != null) {
                this.EditComplete(this, new EventArgs());
            }
        }

        /// <summary>
        /// Targets the checked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.Windows.Forms.ItemCheckedEventArgs"/> instance containing the event data.</param>
        private void TargetChecked(object sender, ItemCheckedEventArgs e) {
            if (e.Item.Checked) {
                this.SelectedTargets.Add(e.Item.Text);
            }
            else {
                this.SelectedTargets.Remove(e.Item.Text);
            }
        }

    }
}
